package org.apogee.mil.client.main.pages.trainee;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.main.pages.trainee.forms.SelectionForm;
import org.apogee.mil.client.main.pages.trainee.widgets.PrevTestsWidget;
import org.apogee.mil.client.main.pages.trainee.widgets.TraineeMessageWidget;
import org.apogee.mil.client.utils.Session;
import org.apogee.mil.client.widgets.DialogHelper;
import org.apogee.mil.shared.User;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class TraineePanel extends LayoutContainer {

	private TextField<String> trainer;
	private User currentUser;
	private int trainerId = -1;

	public TraineePanel() {
		this.currentUser = ServerServiceFactory.getUser(Session.getSessionId());
		this.trainer = new TextField<String>();
	}


	@Override
	public void onRender(Element parent, int index) {
		super.onRender(parent, index);

		setLayout(new FitLayout());
		setWidth(400);
		setHeight(400);

		FormPanel fp = new FormPanel();
		fp.setHeading("Training");
		fp.setBorders(true);
		fp.setFrame(true);

		trainer.setValue("You Have Not Been Assigned a Trainer.");
		trainer.setWidth(400);
		trainer.setReadOnly(true);
		trainer.setInputStyleAttribute("font-weight", "bold");
		trainer.setInputStyleAttribute("color", "#5d8aa8");
		trainer.setInputStyleAttribute("text-align", "center");

		try {

			ServerServiceFactory.getTraineeService(currentUser).getTrainer(currentUser.getUserId(), new AsyncCallback<String>() {

				@Override
				public void onSuccess(String result) {
					if (result.equals("")) {
						trainer.setValue("Unable to get Trainer");
						return;
					}
					String trainerName = result.substring(result.indexOf("-") +1).trim();

					try {
						trainerId = Integer.parseInt(result.substring(0, result.indexOf("-")).trim());
					} catch (Exception ex) { }

					trainer.setValue("Your Trainer is " + trainerName);
					GWT.log("Trainer id: " + trainerId);
				}

				@Override
				public void onFailure(Throwable caught) {

				}
			}); 
		} catch (Exception ex) {
			trainer.setValue("Unable to get Trainer");
		}


		ContentPanel cp = new ContentPanel();
		cp.setBorders(true);

		cp.add(trainer);

		fp.add(cp);

		cp = new ContentPanel();
		cp.setBorders(true);
		cp.setHeading("Trainee Options");
		fp.add(cp);

		HorizontalPanel hp = new HorizontalPanel();
		hp.setStyleAttribute("padding", "10px");

		Button button = new Button("Take Practice Test");
		button.setWidth(150);
		button.setStyleAttribute("padding", "10px");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				DialogHelper helper = new DialogHelper("Test Selection", new SelectionForm());
				helper.show();
			}
		});

		hp.add(button);

		button = new Button("Past Tests Scores");
		button.setWidth(150);
		button.setTitle("Preview Past Test Scores");
		button.setStyleAttribute("padding", "10px");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				DialogHelper helper = new DialogHelper("Review Tests", new PrevTestsWidget(currentUser.getUserId()).asWidget());
				helper.show();
			}
		});
		hp.add(button);

		cp.add(hp);

		hp = new HorizontalPanel();
		hp.setStyleAttribute("padding", "10px");

		button = new Button("Send Trainer Message");
		button.setWidth(150);
		button.setStyleAttribute("padding", "10px");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				DialogHelper helper = new DialogHelper("", new TraineeMessageWidget(trainerId).asWidget());
				helper.show();
			}
		});

		hp.add(button);
		cp.add(hp);


		add(fp);
	}

}
